<?php
require_once('../inc/xml2.php');
switch($action){
	/************************************** list BEGIN ************************************************/
	case "list":
	$dwidth=array(0,50,50,60,120,120,250,140,200);

echo <<<EOT
	<div id="smalltab_container"></div>
	<div class="smalltab_line"></div>
	<div class="div_clear" style="height:10px;"></div>
	<div id="t1">
		<form id="templatesform" onsubmit="return false;">
		<div class="tips_1"><b>提示</b><br />
	* 单击风格的演示图片可以预览模板</div>
		<div class="div_clear" style="height:10px;"></div>
		<table class="table_1" width="100%">
			<tr style="font-weight:bold;color:#333333;"><td class="td_6"><div class="rowdiv_0" style="width:{$dwidth[1]}px;">启用</div><div class="rowdiv_0" style="width:{$dwidth[2]}px;">默认</div><div class="rowdiv_0" style="width:{$dwidth[3]}px;">顺序</div><div class="rowdiv_0" style="width:{$dwidth[5]}px;">模板ID</div><div class="rowdiv_0" style="width:{$dwidth[4]}px;">模板预览</div><div class="rowdiv_0" style="width:{$dwidth[6]}px;">模板名称</div><div class="rowdiv_0" style="width:{$dwidth[7]}px;">代表颜色</div><div class="rowdiv_0" style="width:{$dwidth[8]}px;">作者</div></td></tr>
		
EOT;
		$narray = array();
		$dir = '../template';
		$templatedir = dir($dir);
		$xml = new xmlParser();   
  		$defaultstyle="";
		$tmporder=array();
		while($entry = $templatedir->read()) {
			$tpldir = realpath($dir.'/'.$entry);
			if(!in_array($entry, array('.', '..')) && is_dir($tpldir)) {
				if(!empty($cache_template[$entry])){
					$tmporder[$entry]=$cache_template[$entry]['order'];
				}else{
					$tmporder[$entry]='N';
				}
			}
		}
		asort($tmporder);

		foreach($tmporder as $entry=>$order) {
			$tpldir = realpath($dir.'/'.$entry);
			if(!in_array($entry, array('.', '..')) && is_dir($tpldir)) {
				$config=array();
				$xmlstr=@file_get_contents("{$dir}/{$entry}/config.xml");
				if($xmlstr){
					$config= $xml->xml2array($xmlstr);
				}
				$checkbox="<input type='checkbox' name='tkeys[]' value='{$entry}' class='checkbox_css' />";
				$tempname="";
				$color1="";
				$color2="";
				if(!empty($cache_template[$entry])){
					$defaultstyle=$cache_template[$entry]['default'];
					$checkbox="<input type='checkbox' name='tkeys[]' value='{$entry}' checked='true' class='checkbox_css' />";
					$tempname=$cache_template[$entry]['name'];
					$color1=$cache_template[$entry]['color1'];
				}else{
					$tempname=$config['name'];
					$color1=$config['style']['color1'];
				}
				$orderv = $cache_template[$entry]['order'];
				echo("<tr><td class=\"row_0\" style=\"line-height:150%;\">".
				"<div class='rowdiv_0' style='width:{$dwidth[1]}px;'>{$checkbox}</div>".
				"<div class='rowdiv_0' style='width:{$dwidth[2]}px;'><input type='radio' name='tdefault' value='{$entry}' class='radio_css' /></div>".
				"<div class='rowdiv_0' style='width:{$dwidth[3]}px;'><input type='text' size='4' name='torder[{$entry}]' value='{$orderv}' class=\"text_css\" /></div>".
				"<div class='rowdiv_0' style='width:{$dwidth[5]}px;'>{$entry}</div>".
				"<div class='rowdiv_0' style='width:{$dwidth[4]}px;'><a href='../index.php?styleid={$entry}&preview=1' target='_blank'><img src='{$dir}/{$entry}/{$config['preview']}' border='0' /></a>&nbsp;</div>".
				"<div class='rowdiv_0' style='width:{$dwidth[6]}px;'><input type='text' size='30' value='{$tempname}' name='tname[{$entry}]' class=\"text_css\" /></div>".
				"<div class='rowdiv_0' style='width:{$dwidth[7]}px;'><input type='text' class='text_css' name='color1[{$entry}]' id='color1[{$entry}]' value='{$color1}' size='8'  onfocus='curInput=E(\"color1[{$entry}]\"); colorpicker.choose(event);' style='color:{$color1}' /> <img src='../images/ico_color.gif' align='absmiddle' border='0' id='img_color' class='colorpicker' onclick='curInput=E(\"color1[{$entry}]\"); colorpicker.choose(event);' /></div>".
				"<div class='rowdiv_0' style='width:{$dwidth[8]}px;'>{$config['author']}</div></td></tr>");
				
			}
		}
			
echo <<<EOT
		</table>
EOT;
if(!empty($tmporder)){
echo <<<EOT
		<table class="table_1" width="100%">
			<tr><td class="td_3"><input class="button_css" type="button" value="  提  交  " onclick="ajax_templates()" /></td></tr>
		</table>
EOT;
}
echo <<<EOT
		</form>
	</div>
	<div id="t2"></div>
	<div id="t3"></div>
	<div class="div_clear" style="height:30px;"></div>
<script>
var curInput;
var hasErr=false;
var smallNowTab;
var pt = new Tabs();
pt.classpre="smalltab_";
pt.container = "smalltab_container";
pt.createTab("t1","论坛模板","",true,"n");
pt.init = function(){
	smallNowTab = pt.nowTab;
};
pt.onclick = function(){
	smallNowTab = pt.nowTab;
};		
pt.initTab();
pt.clickNowTab();

setRadioValue("tdefault","{$defaultstyle}");

function ajax_templates(){
	popwin.loading();
	ajaxPost("templatesform","template_ajax.php?action=install",ajax_templates_callback);
}
function ajax_templates_callback(data){
	var btns=[{value:" 确 定 ",onclick:"popwin.close();mainifm.location.reload();",focus:true}];
	popwin.loaded();
	if(data=="y"){
		popwin.showDialog(1,"编辑成功","模板设置成功",btns,280,130);
	}else{
		popwin.showDialog(0,"操作失败","操作失败:<br />"+data,btns,280,130);
	}
}

colorpicker.previewColor = function(color){
	color=color==""?"#000000":color;
	if(curInput){
		curInput.style.color = color;
		curInput.value = color;
	}
}
colorpicker.chooseColor = function(color){
	color=color==""?"#000000":color;
	if(curInput){
		curInput.style.color = color;
		curInput.value = color;
	}
}

</script>

EOT;
	break;
	/************************************** list END ************************************************/


	/************************************** varlist BEGIN ************************************************/
	case "varlist":
	$rows=$cache_template;
	$dwidth=array(0,40,40,200,80,180,150,90);

echo <<<EOT
	<div id="smalltab_container"></div>
	<div class="smalltab_line"></div>
	<div class="div_clear" style="height:10px;"></div>
	<div id="t1">
		<form id="anclistform" onsubmit="return false;">
		<div class="div_clear" style="height:10px;"></div>
		<table class="table_1" width="100%">
			<tr style="font-weight:bold;color:#333333;"><td class="td_6"><div class="rowdiv_0" style="width:{$dwidth[1]}px;"><span class="warning">删除</span></div><div class="rowdiv_0" style="width:{$dwidth[3]}px;">变量</div><div class="rowdiv_0" style="width:{$dwidth[4]}px;">类型</div><div class="rowdiv_0" style="width:{$dwidth[5]}px;">显示位置 / 展示时间</div><div class="rowdiv_0" style="width:{$dwidth[6]}px;">作者 / 发布时间</div><div class="rowdiv_0" style="width:{$dwidth[7]}px;">操作</div></td></tr>
		</table>
		<div id="linksdiv">
EOT;
			foreach($rows as  $row){
				
				$row['posttime']=getDateStr($row['posttime']);
				$row['starttime']=empty($row['starttime'])?"无限制":getDateStr($row['starttime'],'dateonly',false);
				$row['stoptime']=empty($row['stoptime'])?"无限制":getDateStr($row['stoptime'],'dateonly',false);
				$row['title']=htmlFilter($row['title']);
				$row['username']=htmlFilter($row['username']);
				$rowtype = numFilter($row['type'])==1?'链接':'文本';
				$rowlink = numFilter($row['type'])==1? $row['url']:"../announce.php?id=$row[id]";
				$rowforum=$row['fid']==0?"自定义":$cache_forums[$row['fid']]['forumname'];

				echo("<table class=\"table_1\" width=\"100%\"><tr><td class=\"td_6\">
				<div class=\"rowdiv_0\" style=\"width:{$dwidth[1]}px;\"><input type=\"checkbox\" value=\"{$row['id']}\" name=\"delid[]\" class=\"checkbox_css\" /></div>
				<div class=\"rowdiv_0\" style=\"width:{$dwidth[3]}px;\"><a class=\"title\" href=\"{$rowlink}\" target=\"_blank\"><span style=\"color:{$row['color']}\">{$row['title']}</span></a>&nbsp;</div>
				<div class=\"rowdiv_0\" style=\"width:{$dwidth[4]}px;\">{$rowtype}</div>
				<div class=\"rowdiv_0\" style=\"width:{$dwidth[5]}px;\">{$rowforum}<br /><span class=\"time\">{$row['starttime']}</span> ~ <span class=\"time\">{$row['stoptime']}</span></div>
				<div class=\"rowdiv_0\" style=\"width:{$dwidth[6]}px;\"><a href=\"../user.php?uid={$row['userid']}\" target=\"_blank\">{$row['username']}</a><br /><span class=\"time\">{$row['posttime']}</span></div>
				<div class=\"rowdiv_0\" style=\"width:{$dwidth[7]}px;\"><a href=\"admin.php?inc=ad&action=editanc&ancid={$row['id']}\">编辑</a></div></td></tr></table>");	
			}
			
echo <<<EOT
		</div>
			<table class="table_1" width="100%">
			<tr><td class="td_6"><a class="td_5_1a" href="admin.php?inc=template&action=editvar"><img src="images/ico_add.gif" border="0" /> 添加模板变量</a></td></tr>
		</table>
		</form>
	<table width=100%><tr><td><input class="button_css" type="button" value="  提  交  " onclick="ajax_doannounces()" /></td><td></td></tr></table>

	</div>
	<div id="t2"></div>
	<div id="t3"></div>
	<div class="div_clear" style="height:30px;"></div>
<script>

var smallNowTab;
var pt = new Tabs();
pt.classpre="smalltab_";
pt.container = "smalltab_container";
pt.createTab("t1","模板变量管理","",true,"n");
pt.init = function(){
	smallNowTab = pt.nowTab;
};
pt.onclick = function(){
	smallNowTab = pt.nowTab;
};		
pt.initTab();
pt.clickNowTab();


function ajax_doannounces(){
	popwin.loading();
	ajaxPost("anclistform","ad_ajax.php?action=doannounces",ajax_doannounces_callback);
}


function ajax_doannounces_callback(data){
	var btns=[{value:" 确 定 ",onclick:"popwin.close();mainifm.location.reload();",focus:true}];
	popwin.loaded();
	if(data=="y"){
		popwin.showDialog(1,"操作成功","模板变量管理成功",btns,280,130);
	}else{
		popwin.showDialog(0,"操作失败","操作失败:<br />"+data,btns,280,130);
	}
}

</script>

EOT;
	break;
	/************************************** listanc END ************************************************/


}	
?>